<?php
	/**
	 * @package AmpleHarvest
	 */
	require_once 'TableHeader.php';

	/**
	 * @brief This class Describes the Last_Indexed Table in the AmpleHarvest Database
	 *
	 * - Revision History
	 *  - 0.1
	 * 	 - <b>Date</b>: 24 MAR 2011
	 * 	 - <b>Author</b>: Randy Blancett
	 * 	  - Initial Version
	 *
	 * @author Randy Blancett
	 * @version 0.1
	 * @ingroup code classes table
	 */
	class cTABLE_Last_Indexed extends cTable
	{
		/**
		 * @{
		 * @name Columns
		 */
		public $m_int_COL_ID = null;
		public $m_int_COL_Revision = null;
		public $m_int_COL_Date = null;
		public $m_int_COL_Status = null;
		/**
		 * @}
		 */

		/**
		 * @{
		 * @name Status
		 */
		/**
		 * This Option shows that the entry has had no processing done to it.
		 * - Type: integer
		 */
		const C_INT_STATUS_NEW = 0;
		/**
		 * This Option shows that the entry is currently being processed.
		 * - Type: integer
		 */
		const C_INT_STATUS_PROCESSING = 1;
		/**
		 * This Option shows that the entry has been successfuly processed.
		 * - Type: integer
		 */
		const C_INT_STATUS_SUCCESS = 2;
		/**
		 * This Option shows that the entry has had an error while processing.
		 * - Type: integer
		 */
		const C_INT_STATUS_ERROR = 3;

		/**
		 * @}
		 */

		/**
		 * This constructor sets all the required column data
		 */
		public function __construct()
		{
			$this->m_str_TableName = "last_indexed";
			$this->set_Engine(self::C_STR_ENGINE_INNODB);

			//Create Column Objects
			$obj_COL_ID = new cColInt("id");
			$obj_COL_ID->set_Length(10);
			$obj_COL_ID->set_Index(abs_Column::c_int_INDEX_PRIMARY);
			$obj_COL_ID->set_Comment("This column holds the ID of the page that was indexed.");
			$obj_COL_ID->set_Editable(false);
			$obj_COL_ID->set_Label("ID");

			$obj_COL_Revision = new cColInt("revision");
			$obj_COL_Revision->set_Length(10);
			$obj_COL_Revision->set_Comment("This column holds the revision that was indexed.");
			$obj_COL_Revision->set_Label("Revision");

			$obj_COL_Date = new cColTimeStamp("date");
			$obj_COL_Date->set_Null(false);
			//$obj_COL_Date->set_Default("CURRENT_TIMESTAMP");
			$obj_COL_Date->set_Comment("This column holds the Date that the column was last touched.");
			$obj_COL_Date->set_Label("Time Stamp");

			$obj_COL_Status = new cColSmallInt("status");
			$obj_COL_Status->set_Null(true);
			$obj_COL_Date->set_Default("0");
			$obj_COL_Status->set_Comment("This column holds the Status of a given entry.");
			$obj_COL_Status->set_Label("Status");

			//Set up connection
			$this->m_obj_Connection = new cConnection("AmpleHarvest", "admin");

			//Activate all Debug options
			$this->m_obj_Connection->set_DebugLevel(cLog_Connection_Debug::c_int_LEVEL_BASIC);
			$this->m_obj_Connection->set_LogLevel(cLog_Connection::c_int_LEVEL_BASIC);
			//			$this->m_obj_Connection->activate_Logging();
			//			$this->m_obj_Connection->activate_LogDebug();
			//$this->m_obj_Connection->activate_ScreenDebug();

			$this->m_int_COL_ID = $this->add_Column($obj_COL_ID);
			$this->m_int_COL_Revision = $this->add_Column($obj_COL_Revision);
			$this->m_int_COL_Date = $this->add_Column($obj_COL_Date);
			$this->m_int_COL_Status = $this->add_Column($obj_COL_Status);
		}

		/**
		 * Returns if the id is a valid and existing ID
		 * @param integer $int_User_ID ID of the Page to check (GUID)
		 * @return boolean true if valid false if not
		 */
		public function validate_ID($int_ID)
		{
			// ensure that there is an ID passed and that it is a valid GUID
			if (!empty($int_ID) && is_numeric($int_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $int_ID);
				if ($this->do_Search() > 0)
				{
					return true;
				}
			}
			return false;
		}
		/**
		 * Loads information into object from Database based on ID passed
		 * @param string $str_ID ID of the Index to check (int)
		 * return boolean true if loaded false if not
		 */
		public function load_DataFromID($str_ID)
		{
			$this->clear();

			if ($this->validate_ID($str_ID))
			{
				$this->clear_Query();
				$this->add_ColumnQuery($this->m_int_COL_ID, $str_ID);
				if ($this->do_Search() > 0)
				{
					$obj_RS = $this->get_ResultSet();
					if ($obj_RS[0])
					{
						$this->load_DataFromArray($obj_RS[0]);
						return true;
					}
				}
			}
			return false;
		}

		/**
		 * Saves Information to the Database
		 */
		public function save_Data2DB()
		{
			//			$this->output_Debug_Object_HTML();
			if ($this->validate_ID($this->get_id()))
			{
				return $this->update_Data2DB();
			}
			else
			{
				return $this->insert_Data2DB();
			}

			return false;
		}
		/**
		 * This function is used to update data that is already in the database
		 */
		public function update_Data2DB()
		{
			$this->clear_Update();
			$this->add_ColumnQuery($this->m_int_COL_ID, $this->get_id());

			return parent::update_Data2DB();
		}
		/**
		 * This function is used to insert Data into the database
		 */
		public function insert_Data2DB()
		{
			return parent::insert_Data2DB();
		}
	}
?>
